﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using BlogPlat.Controllers;

using BlogPlat.Model;
using BlogPlat.Model.Abstract;

namespace BlogPlat.Tests.Models
{
    public class FakePostRepository : IPostRepository
    {        
        /*
        public override IQueryable<PostEntryModel> Query()
        {
            return _postEntries.AsQueryable();
        }

        public override List<PostEntryModel> ListEntries()
        {
            return Query().ToList();
        }

        public override PostEntryModel Get(int id)
        {
            return Query().Where(p => p.ID == id).SingleOrDefault();
        }

        public override void Create(PostEntryModel entityToCreate)
        {
            _postEntries.Add(entityToCreate);
        }

        public override void Delete(PostEntryModel entityToDelete)
        {
            _postEntries.Remove(entityToDelete);
        }

        public override void Update(PostEntryModel entityToUpdate)
        {
            PostEntryModel modelToUpdate = Query().Where(p => p.ID == entityToUpdate.ID).SingleOrDefault();

            modelToUpdate.Author = entityToUpdate.Author;
            modelToUpdate.DatePublished = entityToUpdate.DatePublished;
            modelToUpdate.FriendlyUrl = entityToUpdate.FriendlyUrl;
            modelToUpdate.IsDraft = entityToUpdate.IsDraft;
            modelToUpdate.PostContent = entityToUpdate.PostContent;
            modelToUpdate.PostSummary = entityToUpdate.PostSummary;
            modelToUpdate.PostTitle = entityToUpdate.PostTitle;
            modelToUpdate.EnableComments = entityToUpdate.EnableComments;
            modelToUpdate.CategoryID = entityToUpdate.CategoryID;
        }
         */

        private List<Post> _posts;

        public FakePostRepository()
        {
            _posts = new List<Post>();
        }

        public ICollection<Post> ListBlogEntriesArchive(int? year, int? month, int? day)
        {            
            if (year.HasValue)
                _posts = _posts.Where(e => e.DatePublished.Year == year.Value).ToList();
            if (month.HasValue)
                _posts = _posts.Where(e => e.DatePublished.Month == month.Value).ToList();
            if (day.HasValue)
                _posts = _posts.Where(e => e.DatePublished.Day == day.Value).ToList();
            return _posts;
        }  

        public void IncrementViewCount(int postID)
        {
            Post post = _posts.Where(p => p.ID == postID).SingleOrDefault();
            post.ViewCount++;
        }

        public void Add(Post newEntity)
        {
            _posts.Add(newEntity);
        }

        public void Delete(int id)
        {
            Post entityToDelete = _posts.Where(p => p.ID == id).SingleOrDefault();
            _posts.Remove(entityToDelete);
        }

        public Post Get(int id)
        {
            return _posts.Where(p => p.ID == id).SingleOrDefault();
        }

        public ICollection<Post> List()
        {
            return _posts;
        }        

        public void Save()
        {
            // not implemented for testing purpose
        }

        public Post GetByFriendlyUrl(string friendlyUrl)
        {
            return _posts.Where(p => p.FriendlyUrl == friendlyUrl).SingleOrDefault();
        }


        public ICollection<Post> ListNewestPublishedPosts()
        {
            return _posts.Where(p => p.IsDraft == false).OrderByDescending(p => p.DatePublished).ToList();
        }

        public void Update(Post entityToUpdate)
        {
            Post post = _posts.FirstOrDefault(p => p.ID == entityToUpdate.ID);
            _posts.Remove(post);

            post.CategoryID = entityToUpdate.CategoryID;
            post.DatePublished = entityToUpdate.DatePublished;
            post.EnableComments = entityToUpdate.EnableComments;
            post.FriendlyUrl = entityToUpdate.FriendlyUrl;
            post.IsDraft = entityToUpdate.IsDraft;
            post.PostContent = entityToUpdate.PostContent;
            post.PostSummary = entityToUpdate.PostSummary;
            post.PostTitle = entityToUpdate.PostTitle;
            post.ViewCount = entityToUpdate.ViewCount;
            _posts.Add(post);
        }
    }
}
